package com.enjoyor.soa.traffic.rest.efoms.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.enjoyor.soa.traffic.api.dto.efoms.CheckDeviceInfoDto;
import com.enjoyor.soa.traffic.api.dto.efoms.DeviceWranInfoDto;
import com.enjoyor.soa.traffic.api.service.dubbo.efoms.IDubboDeviceWranInfoService;
import com.enjoyor.soa.traffic.rest.efoms.dubbo.IDeviceWranInfoConsumeService;
import com.enjoyor.soa.traffic.rest.efoms.helper.CheckPojoHelper;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.MapHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

@Component
public class DeviceWranInfoConsumeService implements IDeviceWranInfoConsumeService {

    @Reference(version = "1.0.0",timeout = 10000)
    //@Reference(version = "1.0.0", timeout = 10000, url="dubbo://192.168.56.8:21141")
    private IDubboDeviceWranInfoService dubboDeviceWranInfoService;
    
    /**
     * 根据报警编号查询设备报警信息
     * @param devWranId 报警编号
     * @return ResultPojo
     */
    @Override
    public ResultPojo selectDeviceWranInfoById(String devWranId) {
    	ResultPojo resultPojo = new ResultPojo();
        try {
			resultPojo = dubboDeviceWranInfoService.selectDeviceWranInfoById(devWranId);
        }catch (Exception e ){
            e.printStackTrace();
            return ResultHelper.getResult(e,EnumAppCode.OPERATOR_FAILURE);
        }
        return resultPojo;
    }
    
    /**
     * 查询设备报警列表信息
     * @param devWranId 报警编号
     * @param devCategoryCode 设备种类编号
     * @param devTypeCode 设备类型编号
     * @param devId 设备编号
     * @param devName 设备名称
     * @param key 关键字（设备编号或设备名称）
     * @param wranTypeCode 报警类型编号
     * @param wranLevelCode 报警等级编号
     * @param wranContent 报警内容
     * @param isRecover 是否恢复正常
     * @param wranStartTime 报警开始时间
     * @param wranEndTime 报警结束时间
     * @return ResultPojo
     */
    @Override
	public ResultPojo selectDeviceWranInfoList(String devWranId, String devCategoryCode, String devTypeCode,
			String devId, String devName, String key, String wranTypeCode, String wranLevelCode, String wranContent,
			String isRecover, String wranStartTime, String wranEndTime) {
    	ResultPojo resultPojo = new ResultPojo();
        try {
			resultPojo = dubboDeviceWranInfoService.selectDeviceWranInfoList(devWranId, devCategoryCode, devTypeCode,
					devId, devName, key, wranTypeCode, wranLevelCode, wranContent, isRecover, wranStartTime,
					wranEndTime);
		}catch (Exception e ){
            e.printStackTrace();
            return ResultHelper.getResult(e,EnumAppCode.OPERATOR_FAILURE);
        }
        return resultPojo;
    }
    
    /**
     * 查询最新实时报警信息
     * @param devCategoryCode 设备种类编号
     * @param devTypeCode 设备类型编号
     * @param isRecover 是否恢复正常
     * @param wranStartTime 报警开始时间
     * @param wranEndTime 报警结束时间
     * @return ResultPojo
     */
    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultPojo selectDeviceWranReal(String devCategoryCode, String devTypeCode, String isRecover,
			String wranStartTime, String wranEndTime) {
    	ResultPojo resultPojo = new ResultPojo();
        try {
        	if(isRecover == null || isRecover.equals("")) {
        		isRecover = "0";
        	}
			//ResultPojo resultAll = dubboDeviceWranInfoService.selectDeviceWranInfoList(null, devCategoryCode,
			//		devTypeCode, null, null, null, null, null, null, isRecover, wranStartTime, wranEndTime);
			Page page = new Page();
			ResultPojo wranPojo = dubboDeviceWranInfoService.selectDeviceWranInfoPage(page, null, devCategoryCode,
					devTypeCode, null, null, null, null, null, null, isRecover, wranStartTime, wranEndTime);
			Page pageR = (Page)wranPojo.getResultList();
			if(pageR != null  && pageR.getResult() != null && pageR.getResult().size() > 0) {
				List<Map<String, Object>> deviceWranMapList = (List<Map<String, Object>>)pageR.getResult();
				List<DeviceWranInfoDto> resultList = new ArrayList<DeviceWranInfoDto>();
				if(null != deviceWranMapList && deviceWranMapList.size() > 0) {
					Map<String, Object> mapTemp = deviceWranMapList.get(0);
					DeviceWranInfoDto deviceWranInfoDto = MapHelper.map2Bean(mapTemp, DeviceWranInfoDto.class);
					resultList.add(deviceWranInfoDto);
				}
				resultPojo =  ResultHelper.getResult(resultList);
			}else {
				resultPojo =  ResultHelper.getResult("查询数据为空！");
			}
		}catch (Exception e ){
            e.printStackTrace();
            return ResultHelper.getResult(e,EnumAppCode.OPERATOR_FAILURE);
        }
        return resultPojo;
    }
    
	/**
     * 查询设备报警分页信息
     * @param devWranId 报警编号
     * @param devCategoryCode 设备种类编号
     * @param devTypeCode 设备类型编号
     * @param devId 设备编号
     * @param devName 设备名称
     * @param key 关键字（设备编号或设备名称）
     * @param wranTypeCode 报警类型编号
     * @param wranLevelCode 报警等级编号
     * @param wranContent 报警内容
     * @param isRecover 是否恢复正常
     * @param wranStartTime 报警开始时间
     * @param wranEndTime 报警结束时间
     * @return ResultPojo
     */
    @Override
	public ResultPojo selectDeviceWranInfoPage(Page page, String devWranId, String devCategoryCode, String devTypeCode,
			String devId, String devName, String key, String wranTypeCode, String wranLevelCode, String wranContent,
			String isRecover, String wranStartTime, String wranEndTime) {
    	ResultPojo resultPojo = new ResultPojo();
        try {
			resultPojo = dubboDeviceWranInfoService.selectDeviceWranInfoPage(page, devWranId, devCategoryCode,
					devTypeCode, devId, devName, key, wranTypeCode, wranLevelCode, wranContent, isRecover,
					wranStartTime, wranEndTime);
		}catch (Exception e ){
            e.printStackTrace();
            return ResultHelper.getResult(e,EnumAppCode.OPERATOR_FAILURE);
        }
        return resultPojo;
    }
	
    /**
     * 新增设备报警信息
     * @param deviceWranInfoDto
     * @param username 操作人
     * @return ResultPojo
     */
    @Override
    public ResultPojo insertDeviceWranInfo(DeviceWranInfoDto deviceWranInfoDto, String username) {
    	ResultPojo resultPojo = new ResultPojo();
        try {
			resultPojo = dubboDeviceWranInfoService.insertDeviceWranInfo(deviceWranInfoDto, username);
		}catch (Exception e ){
            e.printStackTrace();
            return ResultHelper.getResult(e,EnumAppCode.OPERATOR_FAILURE);
        }
        return resultPojo;
    }

    /**
     * 修改设备报警信息
     * @param deviceWranInfoDto
     * @param username 操作人 
     * @return ResultPojo
     */
    @Override
    public ResultPojo updateDeviceWranInfo(DeviceWranInfoDto deviceWranInfoDto, String username) {
    	ResultPojo resultPojo = new ResultPojo();
        try {
			resultPojo = dubboDeviceWranInfoService.updateDeviceWranInfo(deviceWranInfoDto, username);
		}catch (Exception e ){
            e.printStackTrace();
            return ResultHelper.getResult(e,EnumAppCode.OPERATOR_FAILURE);
        }
        return resultPojo;
    }

    /**
     * 删除设备报警信息
     * @param devWranId 报警编号
     * @param username 操作人
     * @return ResultPojo
     */
    @Override
    public ResultPojo deleteDeviceWranInfo(String devWranId, String username) {
    	ResultPojo resultPojo = new ResultPojo();
        try {
			resultPojo = dubboDeviceWranInfoService.deleteDeviceWranInfo(devWranId, username);
		}catch (Exception e ){
            e.printStackTrace();
            return ResultHelper.getResult(e,EnumAppCode.OPERATOR_FAILURE);
        }
        return resultPojo;
    }
}
